package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.DTO.GoodsQueryDTO;
import com.example.springboot.dao.GoodsDao;
import com.example.springboot.dao.mUserDao;
import com.example.springboot.entity.Goods;
import com.example.springboot.entity.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsDao goodsDao;

    @Resource
    private RedisTemplate redisTemplate;

    @GetMapping
    public Result findAll(){
        return Result.success(goodsDao.findAll());
    }

    @GetMapping("/findName")
    public Result findName(){

        return Result.success(goodsDao.findName());
    }

    @GetMapping("/page")
    public Result findPage(GoodsQueryDTO goodsQueryDTO){
        if(goodsQueryDTO.getCurrentPage()==null||goodsQueryDTO.getCurrentPage()<=0||goodsQueryDTO.getPageSize()<1){
            return Result.error("参数错误");
        }
        Integer currentPage = goodsQueryDTO.getCurrentPage();
        Integer pageSize = goodsQueryDTO.getPageSize();

        PageHelper.startPage(currentPage, pageSize);
        List<Goods> goods = goodsDao.findByOptions(goodsQueryDTO);
        return Result.success(new PageInfo<>(goods));
    }

    @PostMapping
    public Result Save(@RequestBody Goods goods){
        if(goods.getName()==null||goods.getDescription()==null||goods.getPrice()==null){
            //throw new RuntimeException("参数不可为空");
            Result.error("参数不可为空");
        }
        return Result.success(goodsDao.insert(goods));
    }

    @PutMapping
    public Result Updata(@RequestBody Goods goods){
        if(goods.getId()==null){
            //throw new RuntimeException("参数不可为空");
            return Result.error("参数不可为空");
        }
        return Result.success(goodsDao.updata(goods));
    }

    @DeleteMapping("/{id}")
    public Result Delete(@PathVariable Integer id){
        if(id==null||id==0){
            //throw new RuntimeException("参数不可为空");
            Result.error("参数不可为空");
        }
        return Result.success(goodsDao.deleteById(id)==1);
    }
    @GetMapping("/{id}")
    public synchronized Result findOne(@PathVariable Integer id) {
        if(redisTemplate.hasKey("goods:"+id)){
//            System.out.println(redisTemplate.opsForHash().entries("goods:"+id));
            Goods goods = new Goods();
            goods.setId((int)redisTemplate.opsForHash().get("goods:"+id,"id"));
            goods.setName((String) redisTemplate.opsForHash().get("goods:"+id,"name"));
            goods.setPrice((BigDecimal)redisTemplate.opsForHash().get("goods:"+id,"price"));
            goods.setImg((String) redisTemplate.opsForHash().get("goods:"+id,"img"));
            goods.setDescription((String) redisTemplate.opsForHash().get("goods:"+id,"description"));
            goods.setNums((int)redisTemplate.opsForHash().get("goods:"+id,"nums"));
            goods.setStatus((int)redisTemplate.opsForHash().get("goods:"+id,"status"));
            goods.setUnit((String) redisTemplate.opsForHash().get("goods:"+id,"unit"));
            return Result.success(goods);
            //System.out.println(goods.getNums());
        }else{
            Goods goods = goodsDao.getById(id);
            redisTemplate.opsForHash().put("goods:"+id,"id",goods.getId());
            redisTemplate.opsForHash().put("goods:"+id,"name",goods.getName());
            redisTemplate.opsForHash().put("goods:"+id,"price",goods.getPrice());
            redisTemplate.opsForHash().put("goods:"+id,"img",goods.getImg());
            redisTemplate.opsForHash().put("goods:"+id,"description",goods.getDescription());
            redisTemplate.opsForHash().put("goods:"+id,"nums",goods.getNums());
            redisTemplate.opsForHash().put("goods:"+id,"status",goods.getStatus());
            redisTemplate.opsForHash().put("goods:"+id,"unit",goods.getUnit());
            redisTemplate.expire("goods:"+id,10, TimeUnit.SECONDS);
        }
        return Result.success(goodsDao.getById(id));
    }

}
